package com.zhang.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.pojo.PowerBean;
import com.zhang.pojo.RoleBean;
import com.zhang.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.Role;
import java.util.List;

@Api(tags = "角色控制接口")
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @ApiOperation("查询所有角色")
    @GetMapping("findAll")
    public List<RoleBean> findAll(){
        return roleService.findAll();
    }

    @ApiOperation("角色管理")
    @GetMapping("findByPage")
    public IPage<RoleBean> findByPage(int current,
                                      int pageSize){
        return  roleService.findByPage(new Page<>(current,pageSize));
    }

    @ApiOperation("添加角色")
    @PostMapping("add")
    public String add(String name,int[] powerIds){

        try {
            RoleBean roleBean = new RoleBean();
            roleBean.setName(name);
            roleService.add(roleBean,powerIds);
            return "ok";
        } catch (Exception e) {
            return "no";
        }
    }

    @ApiOperation("根据id删除角色")
    @DeleteMapping("del/{id}")
    public String del(@PathVariable("id") int id){
        try {
            roleService.del(id);
            return "ok";
        } catch (Exception e) {
            return "no";
        }
    }

    @ApiOperation("根据id查询角色并查询角色拥有的权限集合")
    @GetMapping("findById")
    public RoleBean findById(int id){
      return  roleService.findById(id);
    }

    @ApiOperation("修改角色权限")
    @PutMapping("update")
    public String update(int roleId,int[] powerIds){
        try {
            roleService.update(roleId, powerIds);
            return "ok";
        } catch (Exception e) {
                return "no";
        }
    }





}
